<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Newhouse extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'newhouse';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'region_text',
        'status_text',
        'covertype_text',
        'recommend_text',
        'flag_text',
        'partner_text',
        'dynatown_text'
    ];


    protected static function init()
    {

    }


    public function getRegionList()
    {
        return ['0' => __('Region 0'), '1' => __('Region 1'), '2' => __('Region 2'), '3' => __('Region 3'), '4' => __('Region 4'), '5' => __('Region 5'), '6' => __('Region 6'), '7' => __('Region 7'), '8' => __('Region 8'), '9' => __('Region 9')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }

    public function getCovertypeList()
    {
        return ['0' => __('Covertype 0'), '1' => __('Covertype 1')];
    }

    public function getRecommendList()
    {
        return ['0' => __('Recommend 0'), '1' => __('Recommend 1')];
    }

    public function getFlagList()
    {
        return ['0' => __('Flag 0'), '1' => __('Flag 1')];
    }

    public function getPartnerList()
    {
        return ['0' => __('Partner 0'), '1' => __('Partner 1')];
    }

    public function getDynatownList()
    {
        return ['0' => __('Dynatown 0'), '1' => __('Dynatown 1'), '2' => __('Dynatown 2'), '3' => __('Dynatown 3')];
    }

    public function getRegionTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['region']) ? $data['region'] : '');
        $list = $this->getRegionList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getStatusList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getCovertypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['covertype']) ? $data['covertype'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getCovertypeList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recommend']) ? $data['recommend'] : '');
        $list = $this->getRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFlagTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
        $list = $this->getFlagList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getPartnerTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['partner']) ? $data['partner'] : '');
        $list = $this->getPartnerList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getDynatownTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['dynatown']) ? $data['dynatown'] : '');
        $list = $this->getDynatownList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStatusAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setRecommendAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setFlagAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setPartnerAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setCovertypeAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function admin()
    {
        return $this->belongsTo('\app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function partnerlabel()
    {
        return $this->belongsTo('NewhouseLabel', 'partnerlabel_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function structure()
    {
        return $this->belongsTo('NewhouseStructure', 'id', 'newhouse_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function structureList()
    {
        return $this->hasMany('NewhouseStructure', 'newhouse_id', 'id')->field('newhouse_id, title, category_id, fan, building, propertyfee_json, property_id, propertytel_json, ambientlabel_ids, price, salelabel_ids, status, flag')->where('flag', '1');
    }

    public function price()
    {
        return $this->belongsTo('NewhousePrice', 'id', 'newhouse_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function layout()
    {
        return $this->belongsTo('NewhouseLayout', 'id', 'newhouse_id', [], 'LEFT')->setEagerlyType(0);
    }
}
